package com.Shop.Center.service;

import com.Shop.Center.entity.User;
import com.Shop.Center.mapper.UserMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class UserService {
    @Autowired
    UserMapper userMapper;
    public int insert(String name,String age,String telephone,String password,String destination){
        User user=new User();
        int id=new IdWorker().hashCode();
        user.setId(id);
        user.setName(name);
        user.setAge(Integer.parseInt(age));
        user.setTelephone(telephone);
        user.setPassword(password);
        user.setDestination(destination);
        user.setPoints(0);
        return userMapper.insert(user);
    }
    public List<User> get(String name){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("name",name);
        return userMapper.selectList(queryWrapper);
    }
    public int update(String name,String age,String telephone,String destination,String points,int id){
        User user=new User();
        user.setId(id);
        user.setPoints(Integer.parseInt(points));
        user.setDestination(destination);
        user.setTelephone(telephone);
        user.setAge(Integer.parseInt(age));
        user.setName(name);
        return userMapper.updateById(user);
    }
    public int update(int user_id, String new_password,String telephone){
        User user=userMapper.selectById(user_id);
        if(user.getTelephone().equals(telephone)){
            user.setPassword(new_password);
        }
        return userMapper.updateById(user);
    }
}
